﻿using ResLibary;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//namespace ResLibary
//{

[System.Serializable]
public class StreamingAssetSettingStateObj
{
    public string m_Name;
    public string m_Type;
    public string m_Path;
    public AssetExistStatusEnum m_ExistStatus = AssetExistStatusEnum.Quote;
}


public class FileStateObj : StreamingAssetSettingStateObj
{
    /// <summary>
    /// 引用次数
    /// </summary>
    public int m_Quote;
    public object m_Asset;

    public FileStateObj() { }
    public FileStateObj(StreamingAssetSettingStateObj stateObj)
    {
        this.m_Name = stateObj.m_Name;
        this.m_Path = stateObj.m_Path;
        this.m_Type = stateObj.m_Type;
        this.m_ExistStatus = stateObj.m_ExistStatus;

    }
}
[CreateAssetMenu(fileName = "StreamingAssetLibarySetting", menuName = "LibaryStreamingAssetSetting")]
public class LibaryStreamingAssetSetting : ScriptableObject
{
    public List<StreamingAssetSettingStateObj> texture2ds;
    public List<StreamingAssetSettingStateObj> sprites;
    public List<StreamingAssetSettingStateObj> textAssets;
    public List<StreamingAssetSettingStateObj> audios;
    public List<StreamingAssetSettingStateObj> videos;


    public void AddResToLibary(StreamingAssetSettingStateObj resourceSetting)
    {
        LibaryTypeEnum libaryStatusEnum;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(resourceSetting.m_Type, out libaryStatusEnum))
            return;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                if (texture2ds == null)
                    texture2ds = new List<StreamingAssetSettingStateObj>();
                if (!texture2ds.Contains(resourceSetting))
                    texture2ds.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_TextAsset:
                if (textAssets == null)
                    textAssets = new List<StreamingAssetSettingStateObj>();
                if (!textAssets.Contains(resourceSetting))
                    textAssets.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_Sprite:
                if (sprites == null)
                    sprites = new List<StreamingAssetSettingStateObj>();
                if (!sprites.Contains(resourceSetting))
                    sprites.Add(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_AudioClip:
                if (audios == null)
                    audios = new List<StreamingAssetSettingStateObj>();
                if (!audios.Contains(resourceSetting))
                    audios.Add(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                if (videos == null)
                    videos = new List<StreamingAssetSettingStateObj>();
                if (!videos.Contains(resourceSetting))
                    videos.Add(resourceSetting);
                break;
        }
    }

    public void DelResToLibary(StreamingAssetSettingStateObj resourceSetting)
    {
        LibaryTypeEnum libaryStatusEnum;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(resourceSetting.m_Type, out libaryStatusEnum))
            return;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                if (texture2ds == null)
                    texture2ds = new List<StreamingAssetSettingStateObj>();
                if (texture2ds.Contains(resourceSetting))
                    texture2ds.Remove(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_TextAsset:
                if (textAssets == null)
                    textAssets = new List<StreamingAssetSettingStateObj>();
                if (textAssets.Contains(resourceSetting))
                    textAssets.Remove(resourceSetting);
                break;

            case LibaryTypeEnum.LibaryType_Sprite:
                if (sprites == null)
                    sprites = new List<StreamingAssetSettingStateObj>();
                if (sprites.Contains(resourceSetting))
                    sprites.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_AudioClip:
                if (audios == null)
                    audios = new List<StreamingAssetSettingStateObj>();
                if (audios.Contains(resourceSetting))
                    audios.Remove(resourceSetting);
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                if (videos == null)
                    videos = new List<StreamingAssetSettingStateObj>();
                if (videos.Contains(resourceSetting))
                    videos.Remove(resourceSetting);
                break;
        }
    }

    public Dictionary<string, Dictionary<string, StreamingAssetSettingStateObj>> GetSettingMessage()
    {
        Dictionary<string, Dictionary<string, StreamingAssetSettingStateObj>> dict = new Dictionary<string, Dictionary<string, StreamingAssetSettingStateObj>>();
        if (texture2ds == null)
            texture2ds = new List<StreamingAssetSettingStateObj>();
        if (textAssets == null)
            textAssets = new List<StreamingAssetSettingStateObj>();
        if (audios == null)
            audios = new List<StreamingAssetSettingStateObj>();
        if (videos == null)
            videos = new List<StreamingAssetSettingStateObj>();
        if (sprites == null)
            sprites = new List<StreamingAssetSettingStateObj>();
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Texture2D]] = GetResourceSetting(texture2ds);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_TextAsset]] = GetResourceSetting(textAssets);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_AudioClip]] = GetResourceSetting(audios);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_VideoClip]] = GetResourceSetting(videos);
        dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Sprite]] = GetResourceSetting(sprites);
        return dict;
    }
    protected Dictionary<string, StreamingAssetSettingStateObj> GetResourceSetting(List<StreamingAssetSettingStateObj> list)
    {

        if (list != null)
        {
            Dictionary<string, StreamingAssetSettingStateObj> returnList = new Dictionary<string, StreamingAssetSettingStateObj>();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] != null && !returnList.ContainsKey(list[i].m_Name))
                    returnList.Add(list[i].m_Name, list[i]);
            }
            return returnList;
        }
        return null;
    }

    public void Clear()
    {
        if (texture2ds == null)
            texture2ds = new List<StreamingAssetSettingStateObj>();
        if (textAssets == null)
            textAssets = new List<StreamingAssetSettingStateObj>();
        if (audios == null)
            audios = new List<StreamingAssetSettingStateObj>();
        if (videos == null)
            videos = new List<StreamingAssetSettingStateObj>();
        if (sprites == null)
            sprites = new List<StreamingAssetSettingStateObj>();
        sprites.Clear();
        texture2ds.Clear();
        audios.Clear();
        videos.Clear();
        textAssets.Clear();
    }
}
//}
